﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;

public partial class Management : System.Web.UI.Page
{
    private Operation op = new Operation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlRoleList.Items.Clear();
            ddlUserlist.Items.Clear();
            ddlUserlist.DataTextField = "userid";
            ddlUserlist.DataValueField = "userid";
            ddlUserlist.DataSource = op.GetUserList();
            ddlUserlist.DataBind();
            rblRoleList.DataTextField = "role";
            rblRoleList.DataValueField = "roleid";
            rblRoleList.DataSource = op.GetRoleList();
            rblRoleList.DataBind();            
            ddlRoleList.DataTextField = "role";
            ddlRoleList.DataValueField = "roleid";
            ddlRoleList.DataSource = op.GetRoleList();
            ddlRoleList.DataBind();
            cblPowers.DataTextField = "power";
            cblPowers.DataValueField = "powerid";
            cblPowers.DataSource = op.GetPowerList();
            cblPowers.DataBind();
        }
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~\BrowserDatas.aspx");        
    }

    protected void btnOK_UserRole_Click(object sender, EventArgs e)
    {
        User user = new User();
        user.Uid = ddlUserlist.SelectedItem.Value;
        user.Roleid = rblRoleList.SelectedItem.Value;
        if (op.UpdateUserRole(user))
        {
            Response.Write("<script language='javascript'>alert('用户角色修改成功！');</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('用户角色修改失败！');</script>");
        }
    }

    protected void btnOK_RolePower_Click(object sender, EventArgs e)
    {
        List<string> powers = new List<string>();
        string role = ddlRoleList.SelectedItem.Text;
        for (int i = 0; i < cblPowers.Items.Count; i++)
        {
            if (cblPowers.Items[i].Selected)
            {
                powers.Add(cblPowers.Items[i].Text);
            }
        }
        if ( op.UpdateRolePower(role, powers))
        {
            Response.Write("<script language='javascript'>alert('角色权限修改成功！');</script>");
        }
        else
        {
            Response.Write("<script language='javascript'>alert('角色权限修改失败！');</script>");
        }
        
    }
}